package com.socket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * Created by peak on 2016/1/21.
 */
public class Test {
    public static void main(String[] args) throws Exception {
       execute("set","women","我们");
    }

    static void execute(String cmd,String key,String value) throws Exception{
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("localhost",11211));
        socket.setSoTimeout(10000);

        PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),"utf-8"),true);
        writer.println(cmd+" "+key+" 0 0 "+value.length()+"\r\n"+value);

        writer.println("get "+key);
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),"utf-8"));
        String line = null;
        while(true){
            line=reader.readLine() ;
            System.out.println(line);
            if("END".equals(line)){
                break;
            }
        }
        socket.close();
    }
}
